#include "StdAfx.h"
#include "AlphaPauseCon.h"
#include "Util/UtilInclude.h"
#include "Workout.h"


#define ARRAY_SIZE(AY)    ( sizeof(AY)/sizeof(AY[0]) )

#ifndef UNDER_CE
#undef	RETAILMSG
#define RETAILMSG
#endif

CRect 	g_VidoRect( 270,130,760,500 );


CAlphaPauseCon::CAlphaPauseCon(HWND hWnd)
:CImproMsgCon( hWnd )
//:CImproveCon( hWnd )
//:CContainer(hWnd)	
{
	m_pFalNum = NULL;
}

CAlphaPauseCon::~CAlphaPauseCon(void)
{
}

BOOL CAlphaPauseCon::InitControls(DWORD dwData)
{
	LANGUAGE_DECLARE;
	FONT_DECLARE;
	if(!m_config.Init(L"AlphaPause.xml"))
	{
		g_App.ShowMsgBox(CMD_MSGBOX_OK,L"",LAN_GETTEXT(Msg_ResError));
		return FALSE;
	}

	BOOL bFromDll	= m_config.IsResFromDll();
	wstring str		= m_config.GetImage();	

	wstring strResPath = TEXT("AlphaPause\\");

	HBITMAP hBitmap = CUtil::LoadBitmapEx( strResPath+str,m_config.m_bFromDll);
	SetBackGnd(hBitmap);
	m_rcMsg = m_config.GetRect();
	//SetRect( m_config.GetRect() );
	m_rcRect.SetRect( 0,0,CUtil::GetScreenWidth(),CUtil::GetScreenHeight() );


	m_nIndexCon = IMSGCON_PAUSE;	

	BtnParam btnInfo;
	TextParam txtInfo;
	BatTimePicParam  bat_time_pic;
	IBmpNumInfo      INumInfo;

	CBmpBtn*	pBtn = NULL;
	CText*		pTxt = NULL;


	//m_config.GetBatTimePicParam(bat_time_pic,L"Icon_BACK");
	//m_pPicBK = (CPicture*)this->AddControl(new CPicture(SWS_STATIC,L"",bat_time_pic.m_rc, CMD_WORKOUT_PAUSE_BK));
	//m_config.SetPicParam(m_pPicBK,bat_time_pic);	

	//m_config.GetBatTimePicParam(bat_time_pic,L"AlphaIcon_BACK");
	//m_pAlphaPicBK = (CPicture*)this->AddControl(new CPicture(SWS_STATIC,L"",bat_time_pic.m_rc, CMD_WORKOUT_PAUSE_ALPHABK));
	//m_config.SetPicParam(m_pAlphaPicBK,bat_time_pic);	


	//BatTimePicParam btpInfo;
	//m_config.GetBatTimePicParam(btpInfo,L"Time_Pause");
	//m_pTime = (CTimeCtrl*)AddControl(new CTimeCtrl(SBS_RECT, L"", btpInfo.m_rc, CMD_WORKOUT_PAUSE_TIME));
	//m_config.SetTimeParam(m_pTime,btpInfo);


	BOOL br;
	if( m_config.GetBatTimePicParam(bat_time_pic,L"Icon_BACK") )
	{
		m_pPicIco1 = (CFAlpResIcon*)this->AddBaseIControl( new CFAlpResIcon(SWS_STATIC, L"", bat_time_pic.m_rc,CMD_WORKOUT_PAUSE_BK ) );
		br = m_pPicIco1->LoadRes( CUtil::GetResPath() + strResPath + bat_time_pic.m_strImg );
	}

	IAlpNumInfo iAlphaNumParam;
	m_config.LoadIAlpNumInfo( iAlphaNumParam,TEXT("AlphaNumTime") );
	m_pFalNum = (CFAlpNumCtrl*)this->AddBaseIControl( new CFAlpNumCtrl(SBS_RECT, L"", iAlphaNumParam.m_rc,CMD_WORKOUT_PAUSE_TIME ),LEV_MID );
	m_config.SetAlpNumInfo( m_pFalNum,iAlphaNumParam );

	LoadPauseTime( );
	m_pFalNum->SetValue( m_nPauseTime/60,m_nPauseTime%60 );

	m_config.GetTextParam( txtInfo,TEXT("Title") );
	m_pTxtTitle = (CText*)AddControl(new CText(SWS_STATIC, LAN_GETTEXT(txtInfo.m_strLangKey), txtInfo.m_rc, CMD_WORKOUT_PAUSE_TITLE ));
	m_config.SetTextParam(m_pTxtTitle,txtInfo);


	m_config.GetTextParam( txtInfo,TEXT("TxtMsg") );
	m_pTxtMsg = (CText*)AddControl(new CText(SWS_STATIC, LAN_GETTEXT(txtInfo.m_strLangKey), txtInfo.m_rc, CMD_WORKOUT_PAUSE_MSG ));
	m_config.SetTextParam(m_pTxtMsg,txtInfo);


	return TRUE;
}

void CAlphaPauseCon::OnCommand(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
	int nNum;

}

void  CAlphaPauseCon::Paint(CDC *pDC)
{

}

void	CAlphaPauseCon::UseApiAlphaBlend( CDC *pDC )
{
	//HBITMAP hbmpBK,hBmpBKOld;
	//hbmpBK = GetBackGnd();
	//if (NULL==hbmpBK)
	//	return ;

	CDC *pDCBK = pDC;
	//CDC memdc;
	//memdc.CreateCompatibleDC( pDC->m_hDC );
	//HBITMAP	hbmpMem = CreateCompatibleBitmap( pDC->m_hDC,m_rcRect.Width(),m_rcRect.Height() );
	//HBITMAP hbmpOldMem = memdc.SelectBitmap( hbmpMem );
	//memdc.BitBlt( m_rcRect.left,m_rcRect.top,m_rcRect.Width(),m_rcRect.Height(),pDC->m_hDC,m_rcRect.left,m_rcRect.top,SRCCOPY );
	//int nalpha = 180;
	//AlphaBlendMaskBk( &memdc, GetBackGnd(),nalpha );

	CDC dcIco;
	dcIco.CreateCompatibleDC( pDCBK->m_hDC );

	HBITMAP hBmpIcoOld = dcIco.SelectBitmap( m_pAlphaPicBK->GetPic() );
	BLENDFUNCTION blender;
	blender.BlendOp = AC_SRC_OVER;
	blender.BlendFlags = 0;
	blender.SourceConstantAlpha = 0;
	blender.AlphaFormat   =  AC_SRC_ALPHA;

	CRect rcIco = m_pAlphaPicBK->GetRect();
	BOOL bRet = ::AlphaBlend( pDCBK->m_hDC,rcIco.left,rcIco.top,rcIco.Width(),rcIco.Height(),dcIco.m_hDC,
		0,0,rcIco.Width(),rcIco.Height(),blender);

	dcIco.SelectBitmap( hBmpIcoOld );
	dcIco.DeleteDC();

	m_pTime->Paint( pDCBK );
	m_pTxtTitle->Paint( pDCBK );
	m_pTxtMsg->Paint( pDCBK );

	//memdc.SelectBitmap( hbmpOldMem );
	//DeleteObject( hbmpMem );
	//memdc.DeleteDC();

}

void	CAlphaPauseCon::LoadPauseTime( )
{
	CConfig* pCFG = CConfig::GetInstance();
	m_nPauseTime =  pCFG->GetMaxPauseTimes();
	if ( m_nPauseTime == 0 )
	{
		m_nPauseTime = 60;
	}
}

void   CAlphaPauseCon::DecPauseTime( )
{
	--m_nPauseTime;
	if ( m_nPauseTime<=0 )
	{
		SendMessage(m_hWnd,WM_COMMAND, CMD_WORKOUT_END,0 );
		return ;
	}
	m_pFalNum->SetValue( m_nPauseTime/60,m_nPauseTime%60 );

	InvalidateRect( m_hWnd,g_VidoRect,FALSE );
}
